package remote.controls
{
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	
	import remote.collections.IList;
	import remote.containers.Container;
	import remote.enums.ListItemSize;
	import remote.layouts.ILayout;
	import remote.layouts.listLayout.IListLayout;
	import remote.layouts.listLayout.VerticalListLayout;
	import remote.utils.NumberUtil;
	
	/**
	 *
	 * @author 银魂飞雪
	 * @createDate 2011-7-17
	 * */
	public class ListX extends Scroller
	{
		private var _itemRender:Class = DefaultListItemRender;
		private var _dataProvider:IList;
		/**
		 * 已创建到的子项序号 
		 */		
		private var _addedIndex:int = 0;
		
		private var _selectedIndexs:Array;
		private var _content:Container;
		private var _allowMulSelected:Boolean;
		private var _rowHeight:Number = ListItemSize.AUTO;
		private var _rowWidth:Number = ListItemSize.FIT_PARENT;
		public function ListX()
		{
			super();
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			_content = new Container();
			_content.layout = new VerticalListLayout();
		}
		
		private function checkNeedAddItem():void
		{
			var visibleRect:Rectangle = (layout as IListLayout).visibleRectIndex;
			if(visibleRect.bottom > _addedIndex)
			{
				addItemToIndex(visibleRect.bottom);
			}
		}
		
		private function addItemToIndex(endIndex:int):void
		{
			for(var i:int = _addedIndex; i < endIndex; i++)
			{
				content.addChild(createItem(endIndex) as DisplayObject);
			}
			_addedIndex = endIndex;
		}
		
		private function createItem(index:int):IListItemRender
		{
			var item:IListItemRender = new _itemRender() as IListItemRender;
			item.data = _dataProvider.getItemAt(index);
			setItemSize(item as DisplayObject);
			return item;
		}
		
		private function setItemSize(item:DisplayObject):void
		{
			var area:Rectangle = layout.contentRect;
			if(NumberUtil.equal(_rowWidth, ListItemSize.FIT_PARENT))
			{
				item.width = area.width;
			}
			else if(_rowWidth > 0)
			{
				item.width = _rowWidth;
			}
			if(NumberUtil.equal(_rowHeight, ListItemSize.FIT_PARENT))
			{
				item.height = area.height;
			}
			else if(_rowWidth > 0)
			{
				item.height = _rowHeight;
			}
		}
		
		override public function set layout(value:ILayout):void
		{
			if(value is IListLayout)
			{
				_content.layout = value;
			}
			else
			{
				throw new Error("List的布局应该实现ListLayout接口");
			}
		}
	}
}